package cn.lg.soar.system.general.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.ConditionEnum;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.enums.SysConfigTypeEnum;
import cn.lg.soar.system.general.entity.SysConfig;
import cn.lg.soar.system.general.model.ConfigDTO;
import cn.lg.soar.system.general.service.ISysConfigService;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * 系统配置
 * @author luguoxiang
 * @since 2023-07-13
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "系统配置")
@Validated
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/sys-config")
public class SysConfigController extends BaseController<SysConfig, ISysConfigService> {

    @Override
    protected Serializable getId(SysConfig entity) {
        return entity.getCode();
    }

    @GetMapping("{code}")
    public HttpResult<String> get(@PathVariable String code) {
        return HttpResult.data(service.get(code));
    }

    @Log(value = "设置", dataId = "#code", data = "#value")
    @IdempotentByParam(value = "#p0", expire = 30_000)
    @PostMapping("set/{code}")
    public void set(@PathVariable String code, @RequestBody String value) {
        service.set(code, value);
    }


    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Validated SysConfig data) {
        data.setType(SysConfigTypeEnum.other);
        return super.createResult(data);
    }

    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated SysConfig data) {
        data.setType(SysConfigTypeEnum.other);
        return super.updateResult(data);
    }

    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<String> ids) {
        service.removeOtherByIds(ids);
        return HttpResult.success();
    }

    @DetailMapping
    public HttpResult<SysConfig> detail(@RequestParam String id) {
        return HttpResult.data(service.getOtherById(id));
    }

    @QueryMapping
    public HttpResult<PageResult<SysConfig>> query(PageQuery pageQuery, QueryParams params){
        params.addFilter(SysConfig::getType, ConditionEnum.eq, SysConfigTypeEnum.other.getValue());
        return super.queryResult(pageQuery, params);
    }

    @ExportMapping
    public void exports(QueryParams params, HttpServletResponse resp){
        super.exportExcel("导出", params, resp);
    }

    @ImportMapping
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @ExistMapping
    public HttpResult<Boolean> exist(QueryParams params){
        return super.existResult(params);
    }

}

